package com.example.netty.nettyMessagePack;

import com.example.rmi.HelloImpl;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.HashMap;

/**
 * TODO: 这里要写注释的!
 */
public class EchoClientHandler extends ChannelHandlerAdapter {
    private final int sendNumber;

    public EchoClientHandler(int number){
        this.sendNumber= number;
    }

    @Override
    public void channelActive(ChannelHandlerContext context){
//        for (UserInfo userInfo:userInfos()) {
//            context.write(userInfo);
//        }
        HashMap<String,String> map= new HashMap<>();
        map.put("name","bruce");
        map.put("age","30");
        context.write(map);
        context.flush();
    }

    private UserInfo[] userInfos(){
        UserInfo[] userInfos= new UserInfo[sendNumber];
        for (int i = 0; i <sendNumber; i++) {
            userInfos[i]= new UserInfo("bruce"+(i+1),i+1);
        }
        return userInfos;
    }

    @Override
    public void channelRead(ChannelHandlerContext context,Object msg){
        System.out.println("client receive  message :"+msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx){
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext var1, Throwable var2) throws Exception{
        var2.printStackTrace();
        var1.close();
    }
}
